<?php
class SearchController extends AppController {
	public $helpers = array('Html', 'Form', 'Session');
	
	public function searchtitle(){
		$this->loadModel('Post');
		
		//lay gia tri tu textbox search condition
		$condition = $this->request->data['txtSearchCondition'];
		
		//neu da nhap search condition
		if($condition){
			//set session normal search
			$this->Session->write('SearchCondition', $condition);
		}
		
		//neu da dang nhap
		if($this->Session->check("AdminLogin")){
 			$adminLogin = $this->Session->read("AdminLogin");
 			//chuyen sang action manage cua admin de show ket qua
			return $this->redirect(array('controller' => 'admins', 'action' => 'manage', $adminLogin['Admin']['AdminID']));
		}else{
			//neu chua dang nhap, chuyen sang action home de show ket qua
			return $this->redirect(array('controller' => 'posts', 'action' => 'home'));
		}
		
	}
	
	public function advance(){
		
	}
	
	public function doAdvance(){
		//get data from text box and datetime picker
		$title = $this->request->data['txtTitle'];
		$user = $this->request->data['txtUser'];
		$dateFrom = $this->request->data['dtpDateFrom'];
		$dateTo = $this->request->data['dtpDateTo'];
		
		//neu da nhap text box search title
		if($title){
			//set session search title
			$this->Session->write('SearchTitle', $title);
		}else{
			//neu khong nhap textbox search title thi xoa session search title
			$this->Session->delete('SearchTitle');
		}
		
		//neu da nhap text box search by post user
		if($user){
			$this->Session->write('SearchUser', $user);
		}else{
			$this->Session->delete('SearchUser');
		}
		
		//neu da chon ngay thang trong datetime picker date from
		if($dateFrom){
			$this->Session->write('SearchDateFrom', $dateFrom);
		}else{
			$this->Session->delete('SearchDateFrom');
		}
		
		//neu da chon ngay thang trong datetime picker date to
		if($dateTo){
			$this->Session->write('SearchDateTo', $dateTo);
		}else{
			$this->Session->delete('SearchDateTo');
		}
		
		//xoa session search normal new ton tai
		if($this->Session->check('SearchCondition')){
			$this->Session->delete('SearchCondition');
		}
		
		//set sessiton cho biet dang thuc hien advance search
		$this->Session->write('IsAdvanceSearch', true);
		
		//chuyen sang action home de show ket qua
		return $this->redirect(array('controller' => 'posts', 'action' => 'home'));
	}
}